package com.sgb.kjwl.view.ui.activity.self.authentication;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAuth3Binding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.UserAuthMsgEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.TeamComNumEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.swgk.core.util.UiUtil;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.MainEvent;
import com.sgb.kjwl.view.event.self.AuthEvent;
import com.sgb.kjwl.view.ui.activity.self.userInfo.RegisterActivity3;
import com.sgb.kjwl.view.ui.activity.vehicleManagement.AddDriverInfoActivity;
import com.sgb.kjwl.view.ui.activity.vehicleManagement.DriverInfoActivity;
import com.sgb.kjwl.viewmodel.self.AuthModel3;
import com.sgb.link.router.PathLogin;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.DialogHelperNew;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;

@Route(path = PathLogin.AuthActivity3)
public class AuthActivity3 extends AppActivity {

    @Inject
    AuthModel3 viewModel;
    ActivityAuth3Binding binding;
    private UserAuthMsgEntity data;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AuthActivity3.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }



    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(AuthActivity3.this, R.layout.activity_auth3);
        binding.personAnth.setOnClickListener(this);
        binding.companyAnth.setOnClickListener(this);
        binding.driverAnth.setOnClickListener(this);
        binding.include.titleView.setText("认证中心");
        binding.include.ivBack.setOnClickListener(this);
        binding.rlDriverAuth.setOnClickListener(this);
        binding.teamAnth.setOnClickListener(this);
        binding.rlOrganizationAuth.setOnClickListener(this);
        binding.organizationAnth.setOnClickListener(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        viewModel.getUserInfo(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == 1) {
            MToast.showToast(UiUtil.getContext(), "收到");
        }
    }

    @Override
    public void onMultiClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                EventBus.getDefault().postSticky(MainEvent.make(EventConstant.EVENT_MAIN_ENTERPRISE_SWITCHING, 0));
                finish();
                break;
            case R.id.company_anth:
//                if (!data.isPersonAuth()) {
//                    MToast.showToast(AuthActivity3.this, "请先完成个人认证！");
//                } else {
                    RegisterActivity3.start(this, true);
                //}
                break;
            case R.id.driver_anth:
                // 2021-12-04 (紧急需求) 如果未个人认证，也是可以进行司机认证的。
                Intent intent = new Intent(getActivity(), AddDriverInfoActivity.class);
                if (data.getDriverId() != 0) {
                    intent.putExtra("id", data.getDriverId());
                }
                intent.putExtra("name", data.getRealName());
                intent.putExtra("cardId", data.getIdCard());
                startActivity(intent);
                break;
            case R.id.person_anth:
            case R.id.to_person_auth:
                CertificationActivity.start(this, "1");
                break;
            case R.id.action_bar_image_left:
                finish();
                break;
            case R.id.rl_driver_auth:
                if (!data.getIsDriverAuth().equals("NONE")) {
                    Intent intent1 = new Intent(getActivity(), DriverInfoActivity.class);
                    startActivity(intent1);
                }
                break;
            case R.id.ll_company_info:
                if (data == null) return;
                AuthInfoActivity.start(AuthActivity3.this, data.getEnterpriseAuth());
                EventBus.getDefault().postSticky(AuthEvent.make().setMessageTag(EventConstant.EVENT_SELF_AUTH_INFO).setMessageData(data.getEnterprise()));
                break;
            case R.id.team_anth:
                if (!data.isPersonAuth()) {
                    data.setRealName("");
                    data.setIdCard("");
                    data.setTelPhone(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LOGIN_PHONE, ""));
                }
                TeamAuthActivity.start(AuthActivity3.this, data.getRealName(), data.getIdCard(), data.getTelPhone());
                break;

            case R.id.rl_person_auth:
                ActivityJump.jumpToActivity(this, UserAuthDetailActivity.class, Pair.create("userNo", this.data.getNo()));
                break;
            case R.id.rl_team_auth:
                TeamAuthListActivity.start(getActivity(), 0, data.getNo(), data);
                break;
            case R.id.rl_company_auth:
                TeamAuthListActivity.start(getActivity(), 1, data.getNo(), data);
                break;
            case R.id.organization_anth:
                OrganizationAuthActivity.launch(this);
                break;
            case R.id.rl_organization_auth:
                TeamAuthListActivity.start(getActivity(), 2, data.getNo(), data);
                break;
        }
    }


    public void showAuth(UserAuthMsgEntity data) {
        if (AuthActivity3.this.isDestroyed()) {
            Log.i("AuthActivity3", "AuthActivity3 is Destroyed.");
            return;
        }
        this.data = data;
        switch (data.getPersonalAuthAuditStatus()){
            case 0:
                binding.perStatus.setText("未认证");
                binding.perStatus.setTextColor(Color.parseColor("#333333"));
                binding.personAnth.setVisibility(View.VISIBLE);
                binding.text2.setVisibility(View.VISIBLE);
                binding.personName.setVisibility(View.INVISIBLE);
                binding.personNo.setVisibility(View.INVISIBLE);
                binding.personImg.setVisibility(View.INVISIBLE);
                if (binding.rlPersonAuth.isClickable())
                    binding.rlPersonAuth.setClickable(false);
                break;
            case 2:
                binding.perStatus.setText("已认证");
                binding.perStatus.setTextColor(Color.parseColor("#06C764"));
                binding.personAnth.setVisibility(View.INVISIBLE);
                binding.text2.setVisibility(View.INVISIBLE);
                binding.personName.setVisibility(View.VISIBLE);
                binding.personNo.setVisibility(View.VISIBLE);
                binding.personName.setText(data.getRealName());
                binding.personNo.setText(data.getIdCard());
                binding.personImg.setVisibility(View.VISIBLE);
                try {
                    if (binding.personImg != null) {
                        Glide.with(this).load(R.drawable.yirenzheng).into(binding.personImg);
                    }
                } catch (Exception e) {
                }
                binding.rlPersonAuth.setOnClickListener(this);
                break;
            case 1:
                binding.perStatus.setText("审核中");
                binding.perStatus.setTextColor(Color.parseColor("#06C764"));
                binding.personAnth.setVisibility(View.INVISIBLE);
                binding.text2.setVisibility(View.INVISIBLE);
                binding.personName.setVisibility(View.VISIBLE);
                binding.personNo.setVisibility(View.VISIBLE);
                binding.personName.setText(data.getRealName());
                binding.personNo.setText(data.getIdCard());
                binding.personImg.setVisibility(View.VISIBLE);
                try {
                    if (binding.personImg != null) {
                        Glide.with(this).load(R.drawable.mine_shenhezhong).into(binding.personImg);
                    }
                } catch (Exception e) {
                }
                binding.rlPersonAuth.setOnClickListener(this);
                break;
            case 3:
                binding.perStatus.setText("审核失败");
                binding.perStatus.setTextColor(Color.parseColor("#06C764"));
                binding.personAnth.setVisibility(View.INVISIBLE);
                binding.text2.setVisibility(View.INVISIBLE);
                binding.personName.setVisibility(View.VISIBLE);
                binding.personNo.setVisibility(View.VISIBLE);
                binding.personName.setText(data.getRealName());
                binding.personNo.setText(data.getIdCard());
                binding.personImg.setVisibility(View.VISIBLE);
                try {
                    if (binding.personImg != null) {
                        Glide.with(this).load(R.drawable.mine_shenheshibai).into(binding.personImg);
                    }
                } catch (Exception e) {
                }
                binding.rlPersonAuth.setOnClickListener(this);
                break;
        }
//        if (data.isPersonAuth()) {
//
//
//        } else {
//
//        }

    /*    switch (data.getEnterpriseAuth()) {
            case "SUCCESS":
            case "FAIL":
            case "STANDBY":
            case "NONE":*/
        //不管认不认证都显示此状态
        binding.comimg.setVisibility(View.INVISIBLE);
        try {
            Glide.with(this).load(R.drawable.yirenzheng).into(binding.comimg);
        } catch (Exception e) {

        }
        binding.companyAnth.setVisibility(View.VISIBLE);
        binding.text1.setVisibility(View.VISIBLE);
        binding.llShow.setVisibility(View.VISIBLE);
        binding.comName.setVisibility(View.INVISIBLE);
        binding.companyNo.setVisibility(View.INVISIBLE);
        if (null != data.getEnterprise()) {
            binding.comName.setText(data.getEnterprise().getCompanyName());
            binding.companyNo.setText(data.getEnterprise().getCreditCode());
        }
        binding.rlCompanyAuth.setOnClickListener(this);
//            break;
            /*case "SUCCESS":
                binding.comStatus.setVisibility(View.VISIBLE);
                binding.comimg.setVisibility(View.VISIBLE);
                try {
                    Glide.with(this).load(R.drawable.yirenzheng).into(binding.comimg);
                } catch (Exception e) {
                }
                binding.companyAnth.setVisibility(View.INVISIBLE);
                binding.text1.setVisibility(View.INVISIBLE);
                binding.comName.setVisibility(View.VISIBLE);
                binding.companyNo.setVisibility(View.VISIBLE);
                if (null != data.getEnterprise()) {
                    binding.comName.setText(data.getEnterprise().getCompanyName());
                    binding.companyNo.setText(data.getEnterprise().getCreditCode());
                }
                binding.rlCompanyAuth.setOnClickListener(this);
                break;
            case "FAIL":
                binding.comStatus.setVisibility(View.GONE);
                binding.comStatus.setText("认证失败");
                binding.comStatus.setTextColor(Color.parseColor("#FF2928"));
                binding.comimg.setVisibility(View.INVISIBLE);
                Glide.with(this).load(R.drawable.yirenzheng).into(binding.comimg);
                binding.companyAnth.setVisibility(View.INVISIBLE);
                binding.text1.setVisibility(View.INVISIBLE);
                binding.comName.setVisibility(View.VISIBLE);
                binding.companyNo.setVisibility(View.VISIBLE);
                if (null != data.getEnterprise()) {
                    binding.comName.setText(data.getEnterprise().getCompanyName());
                    binding.companyNo.setText(data.getEnterprise().getCreditCode());
                }
                binding.rlCompanyAuth.setOnClickListener(this);
                break;
            case "STANDBY":
                binding.comStatus.setVisibility(View.GONE);
                binding.comStatus.setText("审核中");
                binding.comStatus.setTextColor(Color.parseColor("#5792FD"));
                binding.comimg.setVisibility(View.VISIBLE);
                Glide.with(this).load(R.drawable.shenhezhong).into(binding.comimg);
                binding.companyAnth.setVisibility(View.INVISIBLE);
                binding.text1.setVisibility(View.INVISIBLE);
                binding.comName.setVisibility(View.VISIBLE);
                binding.companyNo.setVisibility(View.VISIBLE);
                if (null != data.getEnterprise()) {
                    binding.comName.setText(data.getEnterprise().getCompanyName());
                    binding.companyNo.setText(data.getEnterprise().getCreditCode());
                }
                binding.rlCompanyAuth.setOnClickListener(this);
                break;
            case "NONE":
                binding.comStatus.setVisibility(View.GONE);
                binding.comStatus.setText("未认证");
                binding.comStatus.setTextColor(Color.parseColor("#333333"));
                binding.comimg.setVisibility(View.INVISIBLE);
                binding.llShow.setVisibility(View.VISIBLE);
                Glide.with(this).load(R.drawable.yirenzheng).into(binding.comimg);
                binding.companyAnth.setVisibility(View.VISIBLE);
                binding.text1.setVisibility(View.VISIBLE);
                binding.comName.setVisibility(View.INVISIBLE);
                binding.companyNo.setVisibility(View.INVISIBLE);
                if (binding.rlCompanyAuth.isClickable())
                    binding.rlCompanyAuth.setClickable(false);
                break;*/
//        }

        // 团队认证
        /*  if (data.getTeamAuthStatus() == 2) {*/
        binding.text2Team.setVisibility(View.VISIBLE);
        //暂时注释
        binding.teamName.setText(data.getContact());
        binding.teamNo.setText(data.getIdNumber());
        binding.teamName.setVisibility(View.INVISIBLE);
        binding.teamNo.setVisibility(View.INVISIBLE);
        binding.teamImg.setVisibility(View.INVISIBLE);
        if (!AuthActivity3.this.isDestroyed()) {
            Glide.with(this).load(R.drawable.yirenzheng).into(binding.teamImg);
        } else {
            Log.i("AuthActivity3", "Picture loading failed,activity is Destroyed");
        }
        binding.rlTeamAuth.setOnClickListener(this);
     /*   } else {
            binding.teamStatus.setVisibility(View.GONE);
            binding.teamStatus.setTextColor(Color.parseColor("#333333"));
            binding.teamAnth.setVisibility(View.VISIBLE);
            binding.text2Team.setVisibility(View.VISIBLE);
            //暂时注释
            binding.teamName.setVisibility(View.INVISIBLE);
            binding.teamNo.setVisibility(View.INVISIBLE);
            binding.teamImg.setVisibility(View.INVISIBLE);
            if (binding.rlTeamAuth.isClickable())
                binding.rlTeamAuth.setClickable(false);
        }*/


        //司机认证
        switch (data.getIsDriverAuth()) {
            case "SUCCESS":
                binding.rlDriverAuth.setVisibility(View.VISIBLE);
                binding.driverStatus.setText("已认证");
                binding.driverStatus.setTextColor(Color.parseColor("#06C764"));
                binding.driverImg.setVisibility(View.VISIBLE);
                Glide.with(this).load(R.drawable.yirenzheng).into(binding.driverImg);
                binding.driverAnth.setVisibility(View.INVISIBLE);
                binding.rlDriverAuth.setOnClickListener(this);
                break;
            case "FAIL":
                binding.rlDriverAuth.setVisibility(View.VISIBLE);
                binding.driverStatus.setText("审核失败");
                binding.driverStatus.setTextColor(Color.parseColor("#FF2928"));
                binding.driverImg.setVisibility(View.GONE);
                Glide.with(this).load(R.drawable.mine_shenheshibai).into(binding.driverImg);
                binding.driverAnth.setVisibility(View.GONE);
                break;
            case "STANDBY":
                binding.rlDriverAuth.setVisibility(View.VISIBLE);
                binding.driverStatus.setText("审核中");
                binding.driverStatus.setTextColor(Color.parseColor("#5792FD"));
                binding.driverImg.setVisibility(View.VISIBLE);
                Glide.with(this).load(R.drawable.mine_shenhezhong).into(binding.driverImg);
                binding.driverAnth.setVisibility(View.INVISIBLE);
                binding.rlDriverAuth.setOnClickListener(this);
                break;
            case "NONE":
                binding.rlDriverAuth.setVisibility(View.VISIBLE);
                binding.driverStatus.setText("未认证");
                binding.driverStatus.setTextColor(Color.parseColor("#333333"));
                binding.driverImg.setVisibility(View.INVISIBLE);
                Glide.with(this).load(R.drawable.yirenzheng).into(binding.driverImg);
                binding.driverAnth.setVisibility(View.VISIBLE);
                if (binding.rlDriverAuth.isClickable())
                    binding.rlDriverAuth.setClickable(false);
                break;
        }
        DialogHelperNew.dismissProgressDialog();
        binding.root.setVisibility(View.VISIBLE);
    }

    public void setTeamNumber(TeamComNumEntity num) {
        if (num.getCountMyTeamNum() == 0) {
            binding.teamStatus.setVisibility(View.INVISIBLE);
        } else {
            binding.teamStatus.setVisibility(View.VISIBLE);
        }
        if (num.getCountMyCompanyNum() == 0) {
            binding.comStatus.setVisibility(View.INVISIBLE);
        } else {
            binding.comStatus.setVisibility(View.VISIBLE);
        }
        binding.teamStatus.setText("已完成" + num.getCountMyTeamNum() + "个认证");
        binding.comStatus.setText("已完成" + num.getCountMyCompanyNum() + "个认证");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        EventBus.getDefault().postSticky(MainEvent.make(EventConstant.EVENT_MAIN_ENTERPRISE_SWITCHING, 0));
    }

    public void setOrganizationNumber(Integer data) {
        binding.organizationStatus.setVisibility(data == 0 ? View.INVISIBLE : View.VISIBLE);
        binding.organizationStatus.setText("已完成" + data + "个认证");

    }
}
